//
//  RegisterStepTwoView.m
//  Pregnant
//
//  Created by xiangming on 14-4-21.
//  Copyright (c) 2014年 elitework. All rights reserved.
//

#import "RegisterStepTwoView.h"
#import "RegisterHelper.h"
#import "AppDelegate.h"

@implementation RegisterStepTwoView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}


- (void)awakeFromNib
{
    [super awakeFromNib];
    self.backgroundColor = [UIColor clearColor];
    [self.pwdField setDelegate:self];
    [self.verifypwdField setDelegate:self];
    [self.nickNameField setDelegate:self];
    [self.invitCodeField setDelegate:self];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                          action:@selector(tapAction:)];
    [self addGestureRecognizer:tap];
}


- (void)tapAction:(UITapGestureRecognizer *)tap
{
    [self.pwdField resignFirstResponder];
    [self.verifypwdField resignFirstResponder];
    [self.nickNameField resignFirstResponder];
    [self.invitCodeField resignFirstResponder];
}



- (IBAction)finishRegister:(id)sender {
    
    [self checkUserInfoWithPassword:self.pwdField.text
                andOncemorePassword:self.verifypwdField.text];
    
}

//检测输入信息的格式，当格式正确事才能保存注册信息，才能进入下一步
- (void)checkUserInfoWithPassword:(NSString *)pwd
              andOncemorePassword:(NSString *)onceMorepwd
{
    [self.pwdField resignFirstResponder];
    [self.verifypwdField resignFirstResponder];
    [self.nickNameField resignFirstResponder];
    
    
    BOOL ispwdMatch = [Utils isLetterAndNumberWithString:pwd from:6 to:12];
    
   [RegisterHelper shareHelper].invitCode = self.invitCodeField.text;
   [RegisterHelper shareHelper].nickName = CHECK_IS_NULL(self.nickNameField.text);
    
    if ([[RegisterHelper shareHelper].nickName  isEqual: @""]) {
        [Utils showAlertViewWithTitle:@"请输入昵称"];
        return;
    }
    
    if(!ispwdMatch){
        [Utils showAlertViewWithTitle:@"密码格式有误,请重新填写"];
        return;
    }
    else{
        if(![pwd isEqualToString:onceMorepwd]){
            [Utils showAlertViewWithTitle:@"密码不一致，请重新填写"];
        }
        else{
            [RegisterHelper shareHelper].password = self.pwdField.text;
            
            if(self.stepBlock){
                self.stepBlock(StepTypeThree);
            }
        }
        
    }
}


#pragma mark - UITextField Delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

@end
